package com.yt.user.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.dto.BaseDTO;
import com.yt.common.enums.CodeEnum;
import com.yt.common.service.IBaseService;
import com.yt.common.utils.StrUtils;
import com.yt.sys.models.dto.ProfessionInfoDTO;
import com.yt.sys.models.service.IProfessionInfoService;
import com.yt.sys.models.vo.ProfessionInfoVO;
import com.yt.user.models.bo.StudentInfoBO;
import com.yt.user.models.bo.StudentInfoSaveBO;
import com.yt.user.models.dto.StudentInfoDTO;
import com.yt.user.models.service.IStudentInfoService;
import com.yt.user.models.vo.StudentInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 * @dese
 * @date 2025/10/23 14:32
 */
@RestController
@RequestMapping("/cms/studentInfo")
@Slf4j
@Api(value = "StudentInfoController", tags = "学生信息接口")
public class StudentInfoController {

    @DubboReference
    private IStudentInfoService studentInfoService;
    @DubboReference
    private IProfessionInfoService professionInfoService;
    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<StudentInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }


       StudentInfoDTO dto = studentInfoService.selectById(id);

        return new Result<>(dto);
    }


    @ApiOperation(value = "添加学生信息", notes = "添加学生信息")
    @PostMapping(value = "/addStudent")
    public Result addCustomer(@RequestBody StudentInfoSaveBO saveBO) {
            boolean flag = studentInfoService.addCus(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改学生信息", notes = "修改学生信息")
    @PostMapping("/updateStudent")
    public Result update(@RequestBody StudentInfoSaveBO saveBO) {
        StudentInfoDTO dto = new StudentInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = studentInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "逻辑删除学生信息", notes = "逻辑删除学生信息")
    @ApiImplicitParam(name = "id", value = "客户信息id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/deleteStudent")
    public Result deleteCustomer(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result(CodeEnum.FAIL_100002);
        }
        Wrapper<StudentInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        StudentInfoDTO dto = studentInfoService.selectOne(wrapper);
        dto.setIsDel(1);
        return new Result<>(studentInfoService.updateById(dto));
    }

    @PostMapping("/queryCustomer")
    @ApiOperation(value = "查询学生信息", notes = "查询学生信息")
    public Result<List<StudentInfoVO>> queryCustomer() {
        List<StudentInfoVO> vosList = studentInfoService.queryStudentList();
        return new Result<>(vosList);
    }
    @PostMapping("/queryStudentPage")
    @ApiOperation(value = "学生信息分页查询", notes = "分页查询学生信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码（默认1）", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数（默认10）", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "学生姓名（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "studentId", value = "学号（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "professionId", value = "专业ID（模糊查询）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态（精确查询）", paramType = "query", dataType = "Integer")
    })
    public Result<Page<StudentInfoVO>> queryStudentPage(@RequestBody StudentInfoBO bo) {
        // 验证分页参数
        if (bo.getPageNo() == null || bo.getPageNo() < 1) {
            bo.setPageNo(1);
        }
        if (bo.getPageSize() == null || bo.getPageSize() < 1 || bo.getPageSize() > 100) {
            bo.setPageSize(10);
        }

        // 构建查询条件
        Wrapper<StudentInfoDTO> wrapper = new EntityWrapper<>();
        // 时间范围筛选
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());
        }
        //按学生姓名模糊查询
        if (bo.getName() != null) {
            wrapper.like("name", bo.getName());
        }
        //按学号模糊查询
        if (bo.getStudentId() != null) {
            wrapper.eq("student_id", bo.getStudentId());
        }
        //按专业id查询
        if (bo.getProfessionId() != null) {
            wrapper.eq("profession_id", bo.getProfessionId());
        }
        // 状态筛选
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        // 按创建时间倒序排列
        wrapper.orderBy("create_time", false);
        // 执行分页查询
        Page<StudentInfoDTO> dtoPage = new Page<>(bo.getPageNo(), bo.getPageSize());
        dtoPage = studentInfoService.selectPage(dtoPage, wrapper);

        // 转换DTO为VO
        Page<StudentInfoVO> voPage = new Page<>();
        BeanUtils.copyProperties(dtoPage, voPage);

        List<StudentInfoVO> voList = dtoPage.getRecords().stream()
                .map(dto -> {
                    StudentInfoVO vo = new StudentInfoVO();
                    BeanUtils.copyProperties(dto, vo);
                    //查询对应的专业信息
                    ProfessionInfoDTO professionInfoDTO=professionInfoService.selectById(vo.getProfessionId());
                    ProfessionInfoVO professionInfoVO=new ProfessionInfoVO();
                    BeanUtils.copyProperties(professionInfoDTO, professionInfoVO);
                    vo.setProfessionInfo(professionInfoVO);
                    return vo;
                })
                .collect(Collectors.toList());

        voPage.setRecords(voList);

        return new Result<>(voPage);
    }
}
